package com.wwp.tbshop.model.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wwp.tbshop.R;
import com.wwp.tbshop.activity.BrandRecommendActivity;
import com.wwp.tbshop.model.bean.BrandRecommendBean;
import com.wwp.tbshop.util.Constant;
import com.wwp.tbshop.util.UiUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by ${wenping} on 6/17/2017.
 */

public class BrandRecommendRVAdapter extends RecyclerView.Adapter {

    private BrandRecommendActivity mBrandRecommendActivity;

    public BrandRecommendRVAdapter(BrandRecommendActivity brandRecommendActivity) {
        mBrandRecommendActivity = brandRecommendActivity;

    }

    private List<BrandRecommendBean.BrandBean.ValueBean> mBrandBeanList = new ArrayList<>();

    public void setBrandBeanList(List<BrandRecommendBean.BrandBean.ValueBean> brandBeanList) {
        for (int i = 0; i < brandBeanList.size(); i++) {
            mBrandBeanList.add(brandBeanList.get(i));
        }
//        mBrandBeanList = brandBeanList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mBrandRecommendActivity, R.layout.item_brand_recommend, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.setData(mBrandBeanList.get(position));
    }

    @Override
    public int getItemCount() {
        if (mBrandBeanList == null) {
            return 0;
        }
        return mBrandBeanList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.item_brand_recommend_iv)
        ImageView mItemBrandRecommendIv;
        @Bind(R.id.item_brand_recommend_tv)
        TextView mItemBrandRecommendTv;
        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void setData(BrandRecommendBean.BrandBean.ValueBean bean) {
            Picasso.with(UiUtil.getContext())
                    .load(Constant.HOST+bean.getPic())
                    .placeholder(R.drawable.pic_default_grid)
                    .error(R.drawable.ic_error_page)
                    .into(mItemBrandRecommendIv);
            mItemBrandRecommendTv.setText(bean.getName());
        }
    }
}
